<template>
  <div class="div">
    <el-tag
      class="tag"
      v-for="(item, index) in tags"
      :key="item.path"
      :closable="item.name !== 'home'"
      :effect="$route.name == item.name ? 'dark' : 'plain'"
      @click="changeMenu(item)"
      @close="handleClose(item, index)"
      size="small"
    >
      {{ item.label }}
    </el-tag>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "commonTag",
  data() {
    return {};
  },
  methods: {
    ...mapMutations(["CLOSETAG"]),
    // 点击tag标签的事件
    changeMenu(item) {
      if (
        this.$route.path !== item.path &&
        !(this.$route.path === "/home" && item.path === "/")
      ) {
        this.$router.push({ name: item.name });
      }
    },
    // 删除tag标签
    handleClose(item, index) {
      //   this.$store.commit("CLOSETAG", item, index);//调用store中的mutation(第一种写法)
      this.CLOSETAG(item); //第二种写法
      // 路由跳转
      this.$route.name == item.name &&
        this.$router.push(this.tags[index - 1].path);
    },
  },
  computed: {
    ...mapState({ tags: (state) => state.tab.tabsList }),
  },
};
</script>

<style lang="less" scoped>
.div {
  margin-bottom: 10px;
  .tag {
    margin-right: 5px;
    cursor: pointer;
  }
}
</style>